const { pool, add, edit } = require('./baseModel.js')

exports.add = async (fields) => {
    return await add('category_info', fields)
}

exports.edit = async (id, fields) => {
    return await edit('category_info', fields, { id })
}

exports.delete = async (id) => {
    return edit('category_info', { isDel: 1 }, { id })
}

exports.check = async (category_name) => {
    let result = await pool.query('select * from category_info where category_name=? and isDel=0', [category_name])
    return result[0]
}

exports.findById = async (id) => {
    let result = await pool.query('select * from category_info where id=? and isDel=0', [id])
    return result[0]
}

exports.findAll = async () => {
    let category_info = await pool.query('select * from category_info where isDel=0')
    let food_info = await pool.query('select * from food_info where isDel=0')
    for (const category of category_info[0]) {
        // console.log(category);
        category.food_info = []
        for (const food of food_info[0]) {
            if (food.cid === category.id) category.food_info.push(food)
        }
    }
    // console.log(category_info[0]);
    return category_info[0]
}